// pages/center/invoice/apply/apply.js
const app = getApp();
const service = require('../../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 发票类型
    type: 1,
    invoiceType:1,
    // 发票信息
    invoice: {
      id: null,
      orderSn: null,
      headName: null,
      type: 1,
      dutyNumber: null,
      money: null,
      address: '',
      mobile: ''
    }
  },

  // 跳转微信发票插件
  toWechatInvoiceHeader() {
    var that = this;
    wx.chooseInvoiceTitle({
      success(res) {
        console.log(res);
        that.setData({
          ['invoice.headName']: res.title,
          ['invoice.dutyNumber']: res.taxNumber,
          ['invoice.type']: res.type==='0'?2:1,
          ['invoice.address']: res.companyAddress,
          ['invoice.mobile']: res.telephone,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      type: options.type,
      ['invoice.id']: options.id,
      ['invoice.orderSn']: options.id,
      ['invoice.money']: options.payMoney,
      invoiceType: options.type
    })
    this.getInvoiceDetails();
  },

  // 获取发票详情
  getInvoiceDetails() {
    var that = this;
    service.getConnect(service.api.getInvoiceDetails, {
      id: this.data.invoice.id
    }).then(function(res) {
      wx.hideLoading();
      res.data.id && that.setData({
        invoice: res.data
      });
    })
  },

  // 发票抬头类型选择
  bindInvoiceTitleTypeChange(e) {
    // console.log(e.currentTarget.dataset.value);
    this.setData({
      ['invoice.type']: e.currentTarget.dataset.value
    })
  },

  // 税号填写
  bindInvoiceHeadNameInput(e) {
    this.setData({
      ['invoice.headName']: e.detail.value
    })
  },

  // 税号填写
  bindInvoiceDutyNumberInput(e) {
    this.setData({
      ['invoice.dutyNumber']: e.detail.value
    })
  },

  // 邮寄地址填写
  bindInvoiceAddressInput(e) {
    this.setData({
      ['invoice.address']: e.detail.value
    })
  },

  // 邮寄地址填写
  bindInvoiceMobileInput(e) {
    this.setData({
      ['invoice.mobile']: e.detail.value
    })
  },

  // 提交开票申请
  bindSubmitInvoice() {
    var that = this;
    var params = this.data.invoice;
    service.getConnect(service.api.submintInvoice, params).then(function(res) {
      wx.hideLoading();
      wx.showToast({
        title: '提交成功~',
      });
      wx.navigateBack();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getInvoiceDetails();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let wxappStyle = wx.getStorageSync('wxappStyle');
    return {
      title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
      path: '/pages/index/index'
    }

  }
})
